package com.pic.mgt.syushin.action.shell;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;

import com.pic.mgt.syushin.action.SyushinBusinessApMgtAction;
import com.pic.mgt.telnetd.AbstractCommandPerformer;

// (送多方新增)
public class SyushinBusinessToApCommandPerformer extends
		AbstractCommandPerformer {
	
	private final String commandName = "apmgt";
	private final String commandDescritpion = "manage the destination of AP system.";
	
	private SyushinBusinessApMgtAction syushinBusinessApMgtAction;

	@Override
	protected void addOption(Options options) {
		options.addOption("add", false, "add destination of AP system in business. (req: bn,an)");
		options.addOption("rmv", false, "remove destination of AP system in business. (req: bn,an)");
		options.addOption("lis", false, "list destinations of AP system in business. (req: bn)");
		options.addOption("bn", true, "assign business name.");
		options.addOption("an", true, "assign AP system name.");
	}

	@Override
	protected String doCommandPerform(CommandLine commandLine) {
		if (commandLine.hasOption("add")) {
			if (!commandLine.hasOption("bn") ||
				!commandLine.hasOption("an")) {
				return
				super.getReplyMessageWithReplyCode(
						"did not provide business name, or AP system name.",
						this.COMMAND_PERFORM_FAILED_CODE);
			}

			// -------------------------------------------
			
			boolean isAddApSystemNameSuccessful =
					this.syushinBusinessApMgtAction.addSyushinBusinessAp(
							commandLine.getOptionValue("bn"),
							commandLine.getOptionValue("an"));
			
			if (isAddApSystemNameSuccessful) {
				return super.getCommandPerformFinishMsg();
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else if (commandLine.hasOption("rmv")) {
			if (!commandLine.hasOption("bn") ||
				!commandLine.hasOption("an")) {
				return
				super.getReplyMessageWithReplyCode(
						"did not provide business name, or AP system name.",
						this.COMMAND_PERFORM_FAILED_CODE);
			}

			// -------------------------------------------

			boolean isDeleteApSystemNameSuccessful =
				this.syushinBusinessApMgtAction.deleteSyushinBusinessAp(
						commandLine.getOptionValue("bn"),
						commandLine.getOptionValue("an"));

			if (isDeleteApSystemNameSuccessful) {
				return super.getCommandPerformFinishMsg();
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else if (commandLine.hasOption("lis")) {
			if (!commandLine.hasOption("bn")) {
				return
				super.getReplyMessageWithReplyCode(
						"did not provide business name.",
						this.COMMAND_PERFORM_FAILED_CODE);
			}
			
			// -------------------------------------------
			
			String apNamesInfoString =
				this.syushinBusinessApMgtAction.getApNameByBizName(
										commandLine.getOptionValue("bn"));
			
			if (null != apNamesInfoString) {
				return getReplyMessageWithReplyCode(apNamesInfoString,
						                            this.COMMAND_PERFORM_SUCCESS_CODE);
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else {
			return
			super.getReplyMessageWithReplyCode(
					"did not specify management action (option: add, rmv or lis).",
					this.COMMAND_PERFORM_FAILED_CODE);
		}
	}

	@Override
	public String getCommandDescription() {
		return this.commandDescritpion;
	}

	@Override
	public String getCommandName() {
		return this.commandName;
	}

	public void setSyushinBusinessApMgtAction(
			SyushinBusinessApMgtAction syushinBusinessApMgtAction) {
		this.syushinBusinessApMgtAction = syushinBusinessApMgtAction;
	}

}
